
export default class EventHandler {
    constructor(target) {
        this._target=target;
        this._arrEvent = [];
    }

    Add(name,listener){
        this._arrEvent.push({name, listener});
        this._target.addEventListener(name, listener);
    }
    Remove(name,listener){
        this._target.removeEventListener(name, listener);
        for(let i = 0, N = this._arrEvent.length; i < N; ++i) {
            const {name: n, listener: l} = this._arrEvent[i];
            if(name === n && listener == l) {
                this._target.removeEventListener(name, listener);
                this._arrEvent.splice(i, 1);
                return;
            }
        };
    }
    RemoveAll(){
        this._arrEvent.forEach(({name, listener}) => {
            this._target.removeEventListener(name, listener);
        });
        this._arrEvent.length = 0;
    }
}
